package org.weishe.log.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/makeData")
public class MakeDataAction {
	private Calendar c = Calendar.getInstance();
	private Date today = null;
	public static Map<Integer, String> ACTION = new HashMap<Integer, String>();

	static {
		ACTION.put(1, "click");// 点击
		ACTION.put(2, "store");// 收藏
		ACTION.put(3, "car");// 购物车
		ACTION.put(4, "buy");// 购买
	}

	private Random random = new Random();

	@RequestMapping(value = "/makeLogs")
	@ResponseBody
	public String makeRequestLog(HttpServletRequest request) {
		System.out.println("1111111111111111");
		makeLog(random.nextInt(2) + 1);

		return "sucess";
	}

	/**
	 * 产生日志
	 */
	private void makeLog(int logType) {
		today = new Date();
		c.setTime(today);
		// String todayPath = "/" + c.get(Calendar.YEAR) + "-" +
		// c.get(Calendar.MONTH) + "-" + c.get(Calendar.DATE);
		String rootPath = "/home/tomcat/usertrack";
		File todayDir = new File(rootPath);
		if (!todayDir.exists()) {
			todayDir.mkdirs();
		}
		// 按时间来，每20s生成一个文件，在这二十秒钟以内，往这个文件中追加写入
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DATE);
		int hour = c.get(Calendar.HOUR);
		int minute = c.get(Calendar.MINUTE);
		int seconds = c.get(Calendar.SECOND);
		int s = (seconds / 20 + 1) * 20;
		String filePath = rootPath + "/" + year + month + day + hour + minute + s + ".txt";
		File file = new File(filePath);

		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();

			}
		}
		int times = random.nextInt(20) + 1;
		String logs = null;
		switch (logType) {
		case 1:
			for (int i = 0; i < times; i++) {
				String log = getIPLog();
				if (logs == null) {
					logs = log;
				} else {
					logs = logs + "\n" + log;
				}
			}
			break;
		case 2:
			for (int i = 0; i < times; i++) {
				String log = getLongitudeLatitudeLog();
				if (logs == null) {
					logs = log;
				} else {
					logs = logs + "\n" + log;
				}
			}
			break;
		default:
			for (int i = 0; i < times; i++) {
				String log = getLongitudeLatitudeLog();
				if (logs == null) {
					logs = log;
				} else {
					logs = logs + "\n" + log;
				}
			}
			break;
		}
		writeLog("\n" + logs, file);
	}

	/**
	 * 产生用户行为格式 userId+time+IP+itemId+actionType <br/>
	 * 此处假设经纬度和Ip的转换方式即为ip的后两位即为经纬度
	 * 
	 * @return
	 */
	private String getIPLog() {
		String log = "";
		int userId = random.nextInt(1000) + 10000000;
		long time = Calendar.getInstance().getTimeInMillis() - random.nextInt(604800000);
		String ip = "192.168." + (random.nextInt(1000) + 1) + "." + (random.nextInt(1000) + 1);
		int itemId = random.nextInt(1000) + 1;
		String actionType = ACTION.get(random.nextInt(ACTION.size()) + 1);
		log = userId + "\t" + time + "\t" + ip + "\t" + itemId + "\t" + actionType;
		return log;
	}

	private void writeLog(String log, File file) {
		byte bytes[] = new byte[1024];
		bytes = log.getBytes(); // 新加的
		int b = log.length(); // 改
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file, true);
			fos.write(bytes, 0, b);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 产生用户行为格式 userId+time+itemId+actionType+Longitude+latitude <br/>
	 * 此处假设经纬度和Ip的转换方式即为ip的后两位即为经纬度
	 * 
	 * @return
	 */
	private String getLongitudeLatitudeLog() {
		String log = "";
		int userId = random.nextInt(1000) + 10000000;
		long time = Calendar.getInstance().getTimeInMillis() - random.nextInt(604800000);
		int longitude = random.nextInt(1000) + 1;
		int latitude = random.nextInt(1000) + 1;

		int itemId = random.nextInt(1000) + 1;
		String actionType = ACTION.get(random.nextInt(ACTION.size()) + 1);
		log = userId + "\t" + time + "\t" + itemId + "\t" + actionType + "\t" + longitude + "\t" + latitude;
		return log;
	}
}
